package com.gdut.simple.model.impl;

import com.gdut.simple.basic.BasicApplication;
import com.gdut.simple.common.LogUtils;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.common.ToastUtils;
import com.gdut.simple.data.entity.Post;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.model.IPostModel;
import com.gdut.simple.model.ISquareModel;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class PostModelImpl implements IPostModel {

    @Override
    public void refresh(User user, int row, RequestCallback<List<Post>> callback) {
        loadMore(user, 1, row, callback);
    }

    @Override
    public void loadMore(User user, int page, int row, final RequestCallback<List<Post>> callback) {
        LogUtils.i("PostModelImpl loadMore user objectId is " + user.getObjectId());
        BmobQuery<Post> query = new BmobQuery<>();
        query.addWhereEqualTo("author", user);
        query.include("author");
        query.setLimit(row);
        query.setSkip((page - 1) * row);
        query.order("-createdAt");
        query.findObjects(new FindListener<Post>() {
            @Override
            public void done(List<Post> list, BmobException e) {
                if (e != null) {
                    if (callback != null) {
                        callback.onFailure(e.getMessage());
                    }
                    return;
                }
                if (callback != null) {
                    callback.onSuccess(list);
                }
            }
        });
    }
}
